package com.hw.one.collect.xinxiang.client;

import com.hw.one.collect.config.ConditionConfig;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

public class XxClient {

	public final String SERVER_IP = ConditionConfig.get("SERVER_IP", "xinxiang");
	public final int SERVER_PORT = Integer.valueOf(ConditionConfig.get("SERVER_PORT", "xinxiang"));
	private Logger log = Logger.getLogger(this.getClass());
	public Object send(List<byte[]> o) {
		if (o == null)
			return null;
		// System.out.println("send date:" + o);
		// log.info("send date:" + o);
		String ret = "";
		Socket socket = null;
		OutputStream socketOut = null;
		InputStream br = null;
		try {
			socket = new Socket(SERVER_IP, SERVER_PORT);
			for (byte[] sendata : o)

				for (int i = 0; i < 5; i++) {
					String data = new String(sendata, "UTF-8");
					log.info("send data:" + data);
					// 发送命令
					socketOut = socket.getOutputStream();

					socketOut.write(sendata);

					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 接收服务器的反馈
					br = socket.getInputStream();
					byte[] v = new byte[100];
					if (br.available() != 0) {
						br.read(v);
						ret = new String(v, "UTF-8").trim();
						
					}
					if (ret.length() != 0) {
						log.info("return data:"+ret);
						break;
					}

				}

		} catch (IOException e) {
			// e.printStackTrace();
			log.info("同服务器交互时" + e.getMessage());
		} finally {
			try {
				socketOut.close();
				br.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return null;

	}

}
